﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace NutriStat.Statistics
{
    public class WHO2007GrowthReference : GrowthReferenceBase
    {
        public static int FlagBMIUpper = 5;
        public static int FlagBMILower = -4;
        public static int FlagWeightHeightUpper = 5;
        public static int FlagWeightHeightLower = -4;
        public static int FlagWeightAgeUpper = 5;
        public static int FlagWeightAgeLower = -5;
        public static int FlagLengthAgeUpper = 3;
        public static int FlagLengthAgeLower = -5;
        public static int FlagHeightAgeUpper = 3;
        public static int FlagHeightAgeLower = -5;
        public static int FlagSSFUpper;
        public static int FlagSSFLower;
        public static int FlagTSFUpper;
        public static int FlagTSFLower;
        public static int FlagHeadUpper;
        public static int FlagHeadLower;
        public static int FlagMUACUpper;
        public static int FlagMUACLower;

        public WHO2007GrowthReference()
            : base()
        {
            Name = "WHO2007";
            FullName = "WHO reference 2007 (61 months to 19 years)";

            HeightAgeLower = 0;
            HeightAgeUpper = 228;
            LengthAgeLower = 0;
            LengthAgeUpper = 228;

            WeightHeightLower = -1;
            WeightHeightUpper = -1;
            WeightLengthLower = -1;
            WeightLengthUpper = -1;

            WeightAgeLower = 0;
            WeightAgeUpper = 228;

            HeadLower = -1;
            HeadUpper = -1;

            BMILower = 0;
            BMIUpper = 228;

            SSFLower = -1;
            SSFUpper = -1;

            TSFLower = -1;
            TSFUpper = -1;

            MUACLower = -1;
            MUACUpper = -1;

            FlagBMIUpper = 3;
            FlagBMILower = -3;
            FlagWeightHeightUpper = 3;
            FlagWeightHeightLower = -3;
            FlagWeightAgeUpper = 3;
            FlagWeightAgeLower = -3;
            FlagLengthAgeUpper = 0;
            FlagLengthAgeLower = 0;
            FlagHeightAgeUpper = 3;
            FlagHeightAgeLower = -3;
            FlagSSFUpper = 0;
            FlagSSFLower = 0;
            FlagTSFUpper = 0;
            FlagTSFLower = 0;
            FlagHeadUpper = 0;
            FlagHeadLower = 0;
            FlagMUACUpper = 0;
            FlagMUACLower = 0;
        }

        public static string CheckBMIFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagBMIUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagBMILower)
                    flag = NutriStringConstants.L;
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public static string CheckWeightHeightFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagWeightHeightUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagWeightHeightLower)
                    flag = NutriStringConstants.L;
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public static string CheckWeightAgeFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagWeightAgeUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagWeightAgeLower)
                    flag = NutriStringConstants.L;
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public static string CheckHeightAgeFlagged(string sd)
        {
            string flag = NutriStringConstants.Empty;
            try
            {
                double stdDev = Convert.ToDouble(sd);
                if (sd == NutriStringConstants.Empty)
                    flag = NutriStringConstants.Empty;
                else if (stdDev >= FlagHeightAgeUpper)
                    flag = NutriStringConstants.H;
                else if (stdDev <= FlagHeightAgeLower)
                    flag = NutriStringConstants.L;
                else
                    flag = NutriStringConstants.OK;
            }
            catch
            {
            }
            return flag;
        }

        public override string GetPercentOfMedianForWeightHeight(double rawScore) { return NutriStringConstants.Undefined; }
        public override string GetPercentOfMedianForWeightAge(double rawScore) { return NutriStringConstants.Undefined; }
        public override string GetPercentOfMedianForHeightAge(double rawScore) { return NutriStringConstants.Undefined; }

        protected override string GetZ(ref double z, ref double p, ref double flag, double rawScore, double age, short gender, short type)
        {
            double Z = 0;
            double P = 0;
            try
            {
                Z = AnthStat.NutriDataCalc.GetZScoreWHO2007(rawScore, age, gender, type, ref flag);
                P = AnthStat.NutriDataCalc.GetPercentile(Z);
                z = Z;
                p = P;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return Z.ToString();
        }
    }
}

